<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: gavin
 * Date: 15-3-26
 * Time: 下午11:12
 */

class Ghc_install extends Foreground_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper('captcha');
        $this->load->model('ghc_options_model');
        $this->load->database();

    }

    public function index()
    {
        //blog
        $sql = sprintf("CREATE TABLE IF NOT EXISTS ghc_blog (qid int(10) NOT NULL AUTO_INCREMENT, title varchar(256) DEFAULT NULL, date bigint(20) DEFAULT NULL, content longtext,author int(10) DEFAULT NULL,  sortid int(10) DEFAULT NULL,  type varchar(32) DEFAULT 'blog',  views bigint(20) DEFAULT NULL,  top enum('n','y') DEFAULT 'n',  hide enum('n','y') DEFAULT 'n',  showimg varchar(512),PRIMARY KEY (qid),  KEY author (author),  KEY sortid (sortid)) ");
        $this->db->query($sql);
        //options
        $sql = sprintf("CREATE TABLE IF NOT EXISTS ghc_options (  oid int(10) NOT NULL AUTO_INCREMENT,  bolt varchar(32) DEFAULT NULL,  value int(10) DEFAULT NULL,  description text,  PRIMARY KEY (oid))");
        $this->db->query($sql);
        //sort
        $sql = sprintf("CREATE TABLE IF NOT EXISTS ghc_sort (  sid int(10) NOT NULL AUTO_INCREMENT,  sortname varchar(64) NOT NULL,  alias varchar(64) DEFAULT NULL,  taxis int(10) DEFAULT NULL,  description text,  PRIMARY KEY (sid))");
        $this->db->query($sql);
        //user
        $sql = sprintf("CREATE TABLE IF NOT EXISTS ghc_user (  uid int(10) NOT NULL AUTO_INCREMENT,  username varchar(32) DEFAULT NULL,  password varchar(64) DEFAULT NULL,  nickname varchar(32)DEFAULT NULL,  role varchar(64) DEFAULT NULL,  photo varchar(256) DEFAULT NULL,  email varchar(64)DEFAULT NULL,  description varchar(256) DEFAULT NULL,  logins int(10) NOT NULL DEFAULT '0',  loginerror int(10) DEFAULT '0',lastlogindate bigint(20),  PRIMARY KEY (uid)) ");
        $this->db->query($sql);

        //limit
        $sql = sprintf("ALTER TABLE ghc_blog  ADD CONSTRAINT ghc_blog_ibfk_1 FOREIGN KEY (author) REFERENCES ghc_user (uid) ON DELETE CASCADE ON UPDATE CASCADE,  ADD CONSTRAINT ghc_blog_ibfk_2 FOREIGN KEY (sortid) REFERENCES ghc_sort (sid) ON DELETE CASCADE ON UPDATE CASCADE;");
        $this->db->query($sql);

        //options data
        $sql = sprintf("INSERT INTO ghc_options (oid, bolt, value, description) VALUES(1, 'indexviews', 0, NULL),(2, 'attacks', 0, NULL);");
        $this->db->query($sql);

        //user data
        $sql = sprintf("INSERT INTO ghc_user (uid, username, password, nickname, role, photo, email, description, logins, loginerror) VALUES(1, 'root', '63a9f0ea7bb98050796b649e85481845', NULL, 'admin', NULL, NULL, NULL, 9, 1);");
        $this->db->query($sql);

        echo "OK";
    }

}